package com.zhku.zeng.util;

import com.zhku.zeng.plugin.entity.TableEntity;
import freemarker.template.TemplateException;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import org.dom4j.DocumentException;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@AllArgsConstructor
@NoArgsConstructor
public class CreatePojo {
    private String directory;

    private String packageName = "pojo";

    private String tableInfoXmlName = "\\tables.xml";

    private String preName;





    public void createPoJo(String baseDir,String directory) throws IOException, TemplateException, ClassNotFoundException, DocumentException {
        File file = new File(baseDir+"\\src\\main\\resources");
        String absolutePath = file.getAbsolutePath();
        System.out.println(file.getAbsolutePath());
        preName = new File(directory).getAbsolutePath() + "\\pojo";
        File file1 = new File(preName);
        if (!file1.exists()) {
            file1.mkdirs();
        }
        ParseXmlUtil parseXmlUtil = new ParseXmlUtil();
        if (tableInfoXmlName== null) {
            tableInfoXmlName = absolutePath + tableInfoXmlName;
        }
        parseXmlUtil.parse(absolutePath + tableInfoXmlName);
        List<TableEntity> tableEntities = parseXmlUtil.getTableEntities();
        Map<String, Object> data = new HashMap<>();
        for (TableEntity tableEntity : tableEntities) {
            data.put("table",tableEntity);
            FreeMarkUtil freeMarkUtil = new FreeMarkUtil(absolutePath);
            freeMarkUtil.parse("template.ftl", new File(directory).getAbsolutePath() + "\\pojo\\user.java", data);
            System.out.println(tableEntities);
        }


    }

//    public static void main(String[] args) throws ClassNotFoundException, TemplateException, DocumentException, IOException {
//        CreatePojo createPojo = new CreatePojo();
//        String s = "com.zhku.zeng";
//        String s2 = "test-plugin-execute";
//        String tempDir = s.replace(".", "\\") +"\\"+ s2.replace("-", "");
////        createPojo.createPoJo(new File("").getAbsolutePath());
//
//
//
//    }
}
